/*========================================================================
#   FileName: config.c
#     Author: lwdthink
#      Email: lwd_xinghuo@126.com
# 
# LastChange: 2013-05-14 11:09:01
========================================================================*/
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"

static Service* servicelist;

static int readFile(FILE* confFile);

const Service* getServiceInfo(char* name)
{
	Service *s = servicelist->next;
	
	while(s != NULL)
	{
		if(strcmp(s->name, name) == 0)
		{
			break;
		}
		s = s->next;
	}
	return s;
}

int init_service_list()
{
	Service *s;
	s = (Service *)malloc(sizeof(Service));
	if(s == NULL)
	{
		fprintf(stderr, "init service list error! file:%s func%s line%d\n",\
			 	__FILE__, __func__, __LINE__);
		return 1;
	}
	s->next = NULL;
	servicelist = s;
	
	FILE *confFile = fopen("./config/config.ini", "r");
	if(confFile == NULL)
	{
		fprintf(stderr, "Open config file error! file:%s func:%s line:%d\n",\
				__FILE__, __func__, __LINE__);
		return -1;
	}
	
	readFile(confFile);
	
	return 0;
}

static int readLine(FILE* confFile, char* line)
{
	int p;
	int i = 0;
	
	while((p = fgetc(confFile)) != EOF)
	{
		if(p == ' ')
		{
			continue;
		}
		else if(p == '#')
		{
			while((p = fgetc(confFile)) != EOF)
				if(p == '\n')
				{
					line[i] = '\0';
					return 0;
				}
		}
		else if(p == '\n')
		{
			line[i] = '\0';
			return 0;
		}
		else
		{
			line[i++] = p;
		}
	}
	line[i] = '\0';
	return 1;	//file is ended
}

static inline void addNode(char* name, char* mpath, char* tpath, char* ppath)
{
	Service *service = (Service *)malloc(sizeof(Service));
	
	bzero(service, sizeof(Service));
	strcpy(service->name, name);
	strcpy(service->mpath, mpath);
	strcpy(service->tpath, tpath);
	strcpy(service->ppath, ppath);
	
	service->next = servicelist->next;
	servicelist->next = service;
}

static int readFile(FILE *confFile)
{
	static int  linenum = 0;
	int  linearray[4] = { 0 };
	char line[4][256];
	int  fileEnded = 0;
	char *ptr;
	char name[NAMESIZE];
	char mpath[PATHSIZE];
	char tpath[PATHSIZE];
	char ppath[PATHSIZE];
	
	while(!fileEnded)
	{
		int i = 0;
		
		bzero(line, 4 * 256);
		bzero(name, NAMESIZE);
		bzero(mpath, PATHSIZE);
		bzero(tpath, PATHSIZE);
		bzero(ppath, PATHSIZE);
		
		while(!fileEnded && i < 4)
		{
			linenum ++;		//record current line number
			fileEnded = readLine(confFile, line[i]);
			if(line[i][0] == '#' || line[i][0] == '\0')
			{
				continue;
			}
			linearray[i] = linenum;
			i++;
		}
		if(i == 0)
		{
			return 0;
		}
		//three properties need three lines
		if(i < 4 && i > 0)
		{
			fprintf(stderr, "ERROR:read config file error!line:%d\n", linenum);
			exit(EXIT_FAILURE);
		}
		//line 0: the name of the mode
		if(line[0][0] == '[' && line[0][strlen(line[0]) - 1] == ']')
		{
			strncpy(name, line[0]+1, strlen(line[0]) - 2);
		}
		else
		{
			fprintf(stderr, "ERROR:read config file error!line:%d\n", \
					linearray[0]);
			fprintf(stderr, "the format of mode-name is not right!\n");
			exit(EXIT_FAILURE);
		}
		//line 1 && line 2 && line3: mpath && tpath
		i = 1;
		while(i < 4)
		{
			ptr = strchr(line[i], '=');
			*ptr = '\0';
			ptr++;
			if(strcmp(line[i], "mpath") == 0 && strlen(mpath) == 0)
			{
				strncpy(mpath, ptr, strlen(ptr));
			}
			else if(strcmp(line[i], "tpath") == 0 && strlen(tpath) == 0)
			{
				strncpy(tpath, ptr, strlen(ptr));
			}
			else if(strcmp(line[i], "ppath") == 0 && strlen(ppath) == 0)
			{
				strncpy(ppath, ptr, strlen(ptr));
			}
			else
			{
				fprintf(stderr, "ERROR:read config file error! line:%d\n",\
					  linearray[i]);
				exit(EXIT_FAILURE);
			}
			i++;
		}
		addNode(name, mpath, tpath, ppath);
	}
	return 0;
}
